<!-- pages/login/components/passoword.vue -->
<script setup>
	import { ref } from 'vue'
	import { loginByPasswordAPI } from '@/apis/user'
	import { useUserStore } from '@/stores'
	const form = ref(null)
	const fromData = ref({
		mobile:'',
		password:''
	})
	//是否选中我同意协议
	const isAgree = ref(false)
	//表单校验规则
	const rules = {
		mobile:{
			rules:[
				{required: true,errorMessage: '请输入手机号码'},
				{pattern:'^1\\d{10}$',errorMessage:'手机号码格式不正确'}
			]
		},
		password:{
			rules:[
				{required: true,errorMessage:'请输入密码'},
				{pattern: '^[a-zA-Z0-9]{8}$', errorMessage: '密码不正确'}
			]
		}
	}
	const changeAgree = () => {
		isAgree.value = !isAgree.value
	}
	
	//表单登录
	const onSubmit = async() => {
		if(isAgree.value === false){
			return uni.showToast({
				icon:'none',
				title:'请勾选我同意协议'
			})
		} 
		try{
			await form.value.validate()
			const {data} = await loginByPasswordAPI(fromData.value)
			const store = useUserStore()
			store.setUser(data)
			uni.showToast({
				title:'登录成功',
				icon:'success'
			})
			// console.log(res,66666)
			uni.switchTab({
				url:'/pages/my/index'
			})
		}catch(e){
			console.log(e)
		}
	}
</script>
<template>
  <uni-forms class="login-form" ref="form" :model="fromData" :rules="rules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
		v-model="fromData.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="password">
      <uni-easyinput
        type="password"
        placeholder="请输入密码"
        :input-border="false"
        placeholder-style="color: #C3C3C5"
		v-model="fromData.password"
      />
    </uni-forms-item>
    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="changeAgree"/>
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onSubmit">登 录</button>
    <navigator hover-class="none" class="uni-navigator" url=" ">
      忘记密码？
    </navigator>
  </uni-forms>
</template>

<script>
export default {
  options: {
    styleIsolation: 'shared'
  }
}
</script>

<style lang="scss">
@import './styles.scss';
</style>